﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace DotNetLib.系统功能.查询被占用的文件夹或文件夹
{
    public class Handle
    {
        private string _name;
        public Handle(string name)
        {
            _name = name;
        }

        private string StartProcess(string fileName)
        {
            Process tool = new Process();
            tool.StartInfo.FileName = @"类库\Handle\handle.exe";
            tool.StartInfo.Arguments = fileName.Substring(fileName.LastIndexOf("\\", StringComparison.Ordinal)+1);
            tool.StartInfo.UseShellExecute = false;
            tool.StartInfo.RedirectStandardOutput = true;
            tool.StartInfo.CreateNoWindow = true;
            tool.Start();
            tool.WaitForExit();

            string outputTool = tool.StandardOutput.ReadToEnd();
            tool.Dispose();
            return outputTool;
            string matchPattern = @"(?<=\s+pid:\s+)\b(\d+)\b(?=\s+)";
            foreach (Match match in Regex.Matches(outputTool, matchPattern))
            {
                //Process.GetProcessById(int.Parse(match.Value)).Kill();
            }
        }

        public string GetHandleList()
        {
            return StartProcess(_name);
        }
    }
}
